import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { login } from "../api/login";

interface IUserInfo {
    id: number;
    name: string;
    age: number;
}

const useUserStore = defineStore("user", () => {
    const token = ref<string>("");
    const userInfo = ref<IUserInfo>();
    const isLogin = computed(() => {
        return token.value != "";
    });

    const loginAction = async (arg:ILoginAcount)=>{
        const {status, message, token: responseToken} = await login(arg)
        console.log("login");
        console.log(status, message,  responseToken);
        if(status >= 200 && status < 300){
            token.value = responseToken as string
            return {
                success: true,
            }
        }
        return {
            success: false,
            message
        }
    }
    const getUserInfoAction = async() => {
        const result = await getUserInfo()
        // console.log('用户基本信息:', result.userInfo)
        if (result.status === 200) {
          userInfo.value = result.userInfo
          role.value = result.roles[0]
    
          // 生成菜单路由表
          genMenuRoutes()
        } else {
          userInfo.value = undefined
          role.value = undefined
        }
      }



    // 只有return中的数据才有响应式
    return {
        token,
        userInfo,
        isLogin,
        loginAction,
    };
})

export default useUserStore;